﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class DDZGameOver : MonoBehaviour
{
    EginUser user = EginUser.Instance;
    public GameObject EveryRound;
    
    //bg
    public GameObject winBg;
    public GameObject loseBg;
    //btn
    public GameObject LeaveGameBtn;
    public GameObject ContinueGameBtn;
    public DdzGameOverBroadcast newpb;

    //lable
    public List<UILabel>  name;
    public List<UILabel> baseScore;
    public List<UILabel> multiply;
    public List<UILabel> coin;
    public List<GameObject> lordObjShow;

   public bool nextover1;
    void Awake()
    {
        
        UIEventListener.Get(LeaveGameBtn).onClick = ButtonClick;
        UIEventListener.Get(ContinueGameBtn).onClick = ButtonClick;

    }
    private void Start()
    {
        StartCoroutine(des());
        if (user.ControlScene == 1)
        {
            LeaveGameBtn.SetActive(false);
            ContinueGameBtn.transform.localPosition = new Vector3(4,-265,0);
        }
        else
        {

        }
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "LeaveGameBtn")
        {
            if (EginUser.Instance.ControlScene == 1)
            {
                EginUser.Instance.logMsg = "好友场不能中途退出，请继续游戏";
                LogMsg();
            }
            else {
                //发送离开消息
                if (EginDDZ.Instance.createrUid == (int.Parse)(EginUser.Instance.Uid))
                {
                    RoomDismissRequest lg = new Pb.RoomDismissRequest { RoomId = EginDDZ.Instance.roomId };
                    SocketManager socketManager = SocketManager.Instance;
                    socketManager.SendProtobuf1(lg, MsgId.RoomDismissRequest);

                }
                else
                {
                    RoomLeaveRequest lg = new Pb.RoomLeaveRequest { RoomId = EginDDZ.Instance.roomId };
                    SocketManager socketManager = SocketManager.Instance;
                    socketManager.SendProtobuf1(lg, MsgId.RoomLeaveRequest);

                }
            }
        }
        else if (button.name == "ContinueGameBtn") {
            //销毁上一局所有状态ui  DDZCards 
            Destroy(GameObject.Find("DDZCards"));
            DDZGame ddzGame = GameObject.Find("Camera").GetComponent<DDZGame>();
            EginDDZ ddz = EginDDZ.Instance;
            int tempIndex = ddz.returnOtherSeatIndex(ddz.lordSid);
            for (int i = 0; i <user.gameUserInfoList.Count; i++) {
                if (ddz.lordSid == user.gameUserInfoList[i].SeatId) {
                    ddzGame.Users[tempIndex].ShowCharacter(0);
                    ddzGame.Users[tempIndex].LordStatus.SetActive(false);
                }
            }
            GameObject Ready = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load(GameDDZPrafabPath.Ready)));
            Destroy(gameObject);
        }

    }

    public void GameoverEvent(bool nextOver, List<GameOverUserInfo> gameOverUserInfo,int baseScorez,int Multiply) {
       
        if (!nextOver)
        {
            //结算界面
            EveryRound.SetActive(false);
            GameObject DDZSettlement = NGUITools.AddChild(gameObject, (GameObject)(Resources.Load(GameDDZPrafabPath.DDZSettlement))); //用牛牛的结算界面
            DDZSettlement.GetComponent<NNSettlement>().LoadInfo(gameOverUserInfo);
        }
        else {
           
            //每局结束界面
            for (int i = 0; i < gameOverUserInfo.Count; i++)
            {
                if (gameOverUserInfo[i].SeatId == EginDDZ.Instance.seatId)
                {
                    if (gameOverUserInfo[i].RoundScore >= 0)
                    {
                        winBg.SetActive(true);
                    }
                    else {
                        loseBg.SetActive(true);
                    }
                }
                for (int j = 0; j < user.gameUserInfoList.Count; j++) {
                    if (user.gameUserInfoList[j].SeatId == gameOverUserInfo[i].SeatId) {
                        name[i].text = user.gameUserInfoList[j].Nickname;
                    }
                }
                baseScore[i].text = baseScorez.ToString();
                multiply[i].text = Multiply.ToString();
                string str = user.ReturnMoney(gameOverUserInfo[i].RoundScore);
                coin[i].text = str;
                if (gameOverUserInfo[i].SeatId == EginDDZ.Instance.lordSid) {
                    lordObjShow[i].SetActive(true);
                }
                
            }
            DDZGame ddzgame = GameObject.Find("Camera").GetComponent<DDZGame>();
            for (int i = 0; i < gameOverUserInfo.Count; i++)
            {
                if (gameOverUserInfo[i].SeatStatus == SEAT_STATUS.Leave && user.ControlScene == 2)
                {
                    for (int j = 0; j < user.gameUserInfoList.Count; j++)
                    {
                        user.gameUserInfoList[j].IsWitness = false;
                        if (gameOverUserInfo[i].SeatId == user.gameUserInfoList[j].SeatId)
                        {
                            user.gameUserInfoList.RemoveAt(j);
                            int tempIndex = EginDDZ.Instance.returnOtherSeatIndex(gameOverUserInfo[i].SeatId);
                            ddzgame.Users[tempIndex].gameObject.SetActive(false);                            
                            break;

                        }
                    }
                }
            }
        }
    }
    void LogMsg()
    {
        GameObject logMsg = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("LogMsg")));
    }

    IEnumerator des() {
        
        yield return new WaitForSeconds(10f);
        if (nextover1) {
            //销毁上一局所有状态ui  DDZCards 
            Destroy(GameObject.Find("DDZCards"));
            DDZGame ddzGame = GameObject.Find("Camera").GetComponent<DDZGame>();
            EginDDZ ddz = EginDDZ.Instance;
            int tempIndex = ddz.returnOtherSeatIndex(ddz.lordSid);
            for (int i = 0; i < user.gameUserInfoList.Count; i++)
            {
                if (ddz.lordSid == user.gameUserInfoList[i].SeatId)
                {
                    ddzGame.Users[tempIndex].ShowCharacter(0);
                    ddzGame.Users[tempIndex].LordStatus.SetActive(false);
                }
            }
            Destroy(gameObject);
        }
        
    }
}